<?php

/**
 * @author Tuan Anh
 * @copyright 2012
 */

class RecordManager{
    private $db;
    
    function __construct($db){
        $this->db = $db;
    }
    
    function insert($info){
        if(!isset($info['uid']))
            $info['uid'] = 0;
        if(!isset($info['lat']))
            $info['lat'] = 0;
        if(!isset($info['lng']))
            $info['lng'] = 0;
        if(!isset($info['image']))
            $info['image'] = null;
        $query = "INSERT INTO record
            (uid, title, contact_name, contact_mobile, description, address, district, province, `area`, lng, lat, post_date, price, image, `type`, who, quantity_need, quantity_now)
            VALUES(".
            $info['uid'].",'".$info['title']."','".$info['contact_name']."','".$info['contact_mobile']."','".$info['description']."','".$info['address']."','".$info['district']."','".$info['province']."','".$info['area']."','".$info['lng']."','".$info['lat']."',CURDATE(),'".$info['price']."','".$info['image']."','".$info['type']."','".$info['who']."','".$info['quantity_need']."','".$info['quantity_now']."')";
//        echo $query;
        $this->db->dbQuery($query);
        return mysql_insert_id();
    }
    
    function detail($rid){
        $query = "SELECT title, record.address, image, post_date, district, province, description, price, area, contact_name, contact_mobile, lat, lng, username FROM record, user WHERE rid = $rid  AND user.uid = record.uid";
        $this->db->dbQuery($query);
        //TO_DO: xu li neu ko rid ko ton tai
        return $this->db->fetchResult();
    }

    function getRecord($rid){
        $query = "SELECT * FROM record WHERE rid=$rid";
        $r = $this->db->dbQuery($query);
        if($r == 1)
            return $this->db->fetchResult();
        else
            return $r;
    }
     function edit($rid, $info){
        $query = "UPDATE record SET title = '".$info['title']."', contact_name = '".$info['contact_name']."', contact_mobile = '".$info['contact_mobile']."', description = '".$info['description']."', address = '".$info['address']."', district = '".$info['district']."', province = '".$info['province']."', area = '".$info['area']."' WHERE rid = $rid";
        $r = $this->db->dbQuery($query);
        return $r;
    }
    function delete($rid){
        $query = "DELETE FROM record WHERE rid = $rid AND uid = ".$_SESSION['uid'];
        $this->db->dbQuery($query);
    }
    
    public function getRecordList(){
//        $query = "SELECT rid, title, address, province, district, area, price, post_date FROM record, province, district WHERE record.pid = province.pid AND record.did = district.did ORDER BY post_date DESC";
        $query = "SELECT rid, title, address, province, district, area, price, post_date FROM record ORDER BY post_date DESC, rid DESC";
        $this->db->dbQuery($query);
        return $this->db->result['content'];
    }

    public function getRecordOfUser($uid){
        $query = "SELECT rid, uid, title, post_date  FROM record WHERE uid = $uid ORDER BY post_date DESC, rid DESC LIMIT 10";
        $this->db->dbQuery($query);
        return $this->db->result['content'];
    }

    public function updatecoor($rid, $lat, $lng){
        $query = "UPDATE record SET lat = $lat, lng = $lng, latrad = RADIANS($lat), lngrad = RADIANS($lng) WHERE rid = $rid";  //echo $query;
        $this->db->dbQuery($query);
    }

    function getRecordOfSearch($province, $district){
        $query = "SELECT rid, title, address, province, district, area, price, post_date FROM record WHERE district = '$district' AND province = '$province' ORDER BY post_date DESC, rid DESC";
//        echo $query;
        if($this->db->dbQuery($query) >= 1){
            return $this->db->result['content'];
        }
        else{
            return 0;
        }

    }
}

?>